from dataclasses import dataclass
import re

@dataclass
class MonitorConfig:
    # 基础配置
    phase: str = "post"               # 采集阶段: pre/post
    include_patterns: tuple = (r".*",) # 参数名称正则匹配
    exclude_patterns: tuple = ()      # 排除参数正则
    metrics: tuple = ("norm", "max", "min", "mean")
    
    # 性能优化
    grad_compression: bool = True     # 梯度张量压缩
    async_io: bool = True             # 异步数据写入
    fp16_stats: bool = True           # FP16计算统计
    
    # 输出配置
    output_dir: str = "./grad_logs"
    log_interval: int = 10            # 记录间隔步数

    def should_monitor(self, param_name: str) -> bool:
        include = any(re.match(p, param_name) for p in self.include_patterns)
        exclude = any(re.match(p, param_name) for p in self.exclude_patterns)
        return include and not exclude
